﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using V5_DBUtility;
using System.Data.SqlClient;
using V5_Model;

using System.Data.OleDb;
namespace V5_DAL.OleDb {
    public class DALProductComment  {

        #region  BaseMethod
        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId() {
            return DbHelperOleDb.GetMaxID("ID", "K_ProductComment");
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int ID) {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from K_ProductComment");
            strSql.Append(" where ID=" + ID);
            return DbHelperOleDb.Exists(strSql.ToString());
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public void Add(ModelProductComment model) {
            StringBuilder strSql = new StringBuilder();
            StringBuilder strSql1 = new StringBuilder();
            StringBuilder strSql2 = new StringBuilder();
            if (model.ID != null) {
                strSql1.Append("ID,");
                strSql2.Append("" + model.ID + ",");
            }
            if (model.CommentContent != null) {
                strSql1.Append("CommentContent,");
                strSql2.Append("'" + model.CommentContent + "',");
            }
            if (model.CommentName != null) {
                strSql1.Append("CommentName,");
                strSql2.Append("'" + model.CommentName + "',");
            }
            if (model.CommentEmail != null) {
                strSql1.Append("CommentEmail,");
                strSql2.Append("'" + model.CommentEmail + "',");
            }
            if (model.CommentIP != null) {
                strSql1.Append("CommentIP,");
                strSql2.Append("'" + model.CommentIP + "',");
            }
            if (model.ProductName != null) {
                strSql1.Append("ProductName,");
                strSql2.Append("'" + model.ProductName + "',");
            }
            if (model.ProductID != null) {
                strSql1.Append("ProductID,");
                strSql2.Append("" + model.ProductID + ",");
            }
            if (model.AddDateTime != null) {
                strSql1.Append("AddDateTime,");
                strSql2.Append("'" + model.AddDateTime + "',");
            }
            if (model.ReplyComment != null) {
                strSql1.Append("ReplyComment,");
                strSql2.Append("'" + model.ReplyComment + "',");
            }
            if (model.ReplyName != null) {
                strSql1.Append("ReplyName,");
                strSql2.Append("'" + model.ReplyName + "',");
            }
            if (model.Pmost != null) {
                strSql1.Append("Pmost,");
                strSql2.Append("" + model.Pmost + ",");
            }
            if (model.MemberID != null) {
                strSql1.Append("MemberID,");
                strSql2.Append("" + model.MemberID + ",");
            }
            if (model.Pmost1 != null) {
                strSql1.Append("Pmost1,");
                strSql2.Append("" + model.Pmost1 + ",");
            }
            if (model.Pmost2 != null) {
                strSql1.Append("Pmost2,");
                strSql2.Append("" + model.Pmost2 + ",");
            }
            if (model.Pmost3 != null) {
                strSql1.Append("Pmost3,");
                strSql2.Append("" + model.Pmost3 + ",");
            }
            if (model.Passed != null) {
                strSql1.Append("Passed,");
                strSql2.Append("" + model.Passed + ",");
            }
            strSql.Append("insert into K_ProductComment(");
            strSql.Append(strSql1.ToString().Remove(strSql1.Length - 1));
            strSql.Append(")");
            strSql.Append(" values (");
            strSql.Append(strSql2.ToString().Remove(strSql2.Length - 1));
            strSql.Append(")");
            int rows = DbHelperOleDb.ExecuteSql(strSql.ToString());
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(ModelProductComment model) {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update K_ProductComment set ");
            if (model.CommentContent != null) {
                strSql.Append("CommentContent='" + model.CommentContent + "',");
            }
            if (model.CommentName != null) {
                strSql.Append("CommentName='" + model.CommentName + "',");
            }
            if (model.CommentEmail != null) {
                strSql.Append("CommentEmail='" + model.CommentEmail + "',");
            }
            if (model.CommentIP != null) {
                strSql.Append("CommentIP='" + model.CommentIP + "',");
            }
            if (model.ProductName != null) {
                strSql.Append("ProductName='" + model.ProductName + "',");
            }
            if (model.ProductID != null) {
                strSql.Append("ProductID=" + model.ProductID + ",");
            }
            if (model.AddDateTime != null) {
                strSql.Append("AddDateTime='" + model.AddDateTime + "',");
            }
            if (model.ReplyComment != null) {
                strSql.Append("ReplyComment='" + model.ReplyComment + "',");
            }
            if (model.ReplyName != null) {
                strSql.Append("ReplyName='" + model.ReplyName + "',");
            }
            if (model.Pmost != null) {
                strSql.Append("Pmost=" + model.Pmost + ",");
            }
            if (model.MemberID != null) {
                strSql.Append("MemberID=" + model.MemberID + ",");
            }
            if (model.Pmost1 != null) {
                strSql.Append("Pmost1=" + model.Pmost1 + ",");
            }
            if (model.Pmost2 != null) {
                strSql.Append("Pmost2=" + model.Pmost2 + ",");
            }
            if (model.Pmost3 != null) {
                strSql.Append("Pmost3=" + model.Pmost3 + ",");
            }
            if (model.Passed != null) {
                strSql.Append("Passed=" + model.Passed + ",");
            }
            int n = strSql.ToString().LastIndexOf(",");
            strSql.Remove(n, 1);
            strSql.Append(" where ID=" + model.ID + " ");
            int rows = DbHelperOleDb.ExecuteSql(strSql.ToString());
            if (rows > 0) {
                return true;
            }
            else {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int ID) {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from K_ProductComment ");
            strSql.Append(" where ID=" + ID);
            int rows = DbHelperOleDb.ExecuteSql(strSql.ToString());
            if (rows > 0) {
                return true;
            }
            else {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public ModelProductComment GetModel(int ID) {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * from K_ProductComment ");
            strSql.Append(" where ID=" + ID);
            ModelProductComment model = new ModelProductComment();
            DataSet ds = DbHelperOleDb.Query(strSql.ToString());
            if (ds != null && ds.Tables[0].Rows.Count > 0) {
                if (ds.Tables[0].Rows[0]["ID"] != null && ds.Tables[0].Rows[0]["ID"].ToString() != "") {
                    model.ID = int.Parse(ds.Tables[0].Rows[0]["ID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["CommentContent"] != null && ds.Tables[0].Rows[0]["CommentContent"].ToString() != "") {
                    model.CommentContent = ds.Tables[0].Rows[0]["CommentContent"].ToString();
                }
                if (ds.Tables[0].Rows[0]["CommentName"] != null && ds.Tables[0].Rows[0]["CommentName"].ToString() != "") {
                    model.CommentName = ds.Tables[0].Rows[0]["CommentName"].ToString();
                }
                if (ds.Tables[0].Rows[0]["CommentEmail"] != null && ds.Tables[0].Rows[0]["CommentEmail"].ToString() != "") {
                    model.CommentEmail = ds.Tables[0].Rows[0]["CommentEmail"].ToString();
                }
                if (ds.Tables[0].Rows[0]["CommentIP"] != null && ds.Tables[0].Rows[0]["CommentIP"].ToString() != "") {
                    model.CommentIP = ds.Tables[0].Rows[0]["CommentIP"].ToString();
                }
                if (ds.Tables[0].Rows[0]["ProductName"] != null && ds.Tables[0].Rows[0]["ProductName"].ToString() != "") {
                    model.ProductName = ds.Tables[0].Rows[0]["ProductName"].ToString();
                }
                if (ds.Tables[0].Rows[0]["ProductID"] != null && ds.Tables[0].Rows[0]["ProductID"].ToString() != "") {
                    model.ProductID = int.Parse(ds.Tables[0].Rows[0]["ProductID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["AddDateTime"] != null && ds.Tables[0].Rows[0]["AddDateTime"].ToString() != "") {
                    model.AddDateTime = Convert.ToDateTime(ds.Tables[0].Rows[0]["AddDateTime"].ToString());
                }
                if (ds.Tables[0].Rows[0]["ReplyComment"] != null && ds.Tables[0].Rows[0]["ReplyComment"].ToString() != "") {
                    model.ReplyComment = ds.Tables[0].Rows[0]["ReplyComment"].ToString();
                }
                if (ds.Tables[0].Rows[0]["ReplyName"] != null && ds.Tables[0].Rows[0]["ReplyName"].ToString() != "") {
                    model.ReplyName = ds.Tables[0].Rows[0]["ReplyName"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Pmost"] != null && ds.Tables[0].Rows[0]["Pmost"].ToString() != "") {
                    model.Pmost = int.Parse(ds.Tables[0].Rows[0]["Pmost"].ToString());
                }
                if (ds.Tables[0].Rows[0]["MemberID"] != null && ds.Tables[0].Rows[0]["MemberID"].ToString() != "") {
                    model.MemberID = int.Parse(ds.Tables[0].Rows[0]["MemberID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Pmost1"] != null && ds.Tables[0].Rows[0]["Pmost1"].ToString() != "") {
                    model.Pmost1 = int.Parse(ds.Tables[0].Rows[0]["Pmost1"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Pmost2"] != null && ds.Tables[0].Rows[0]["Pmost2"].ToString() != "") {
                    model.Pmost2 = int.Parse(ds.Tables[0].Rows[0]["Pmost2"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Pmost3"] != null && ds.Tables[0].Rows[0]["Pmost3"].ToString() != "") {
                    model.Pmost3 = int.Parse(ds.Tables[0].Rows[0]["Pmost3"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Passed"] != null && ds.Tables[0].Rows[0]["Passed"].ToString() != "") {
                    model.Passed = int.Parse(ds.Tables[0].Rows[0]["Passed"].ToString());
                }
                return model;
            }
            else {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere) {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * ");
            strSql.Append(" FROM K_ProductComment ");
            if (strWhere.Trim() != "") {
                strSql.Append(" where  1=1 " + strWhere);
            }
            return DbHelperOleDb.Query(strSql.ToString());
        }
        #endregion  Method

        #region K_ProductComment开始
        public bool DeleteList(string IDlist) {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from K_ProductComment ");
            strSql.Append(" where ID in (" + IDlist + ")  ");
            int rows = DbHelperOleDb.ExecuteSql(strSql.ToString());
            if (rows > 0) {
                return true;
            }
            else {
                return false;
            }
        }

        public DataSet GetList(int Top, string strWhere, string filedOrder) {
            return this.GetList("*", Top, strWhere, filedOrder);
        }

        public DataSet GetList(int PageSize, int PageIndex, string strWhere, string strOrder, ref int RecordCount) {
            return this.GetList("*", PageSize, PageIndex, strWhere, strOrder, ref RecordCount);
        }

        public List<ModelProductComment> GetModelList(string strWhere) {
            DataSet ds = this.GetList(strWhere);
            if (ds != null && ds.Tables[0].Rows.Count > 0) {
                return DBCommonHelper.FillModel<ModelProductComment>(ds);
            }
            return null;
        }

        public List<ModelProductComment> GetModelList(int Top, string strWhere, string filedOrder) {
            DataSet ds = this.GetList(Top, strWhere, filedOrder);
            if (ds != null && ds.Tables[0].Rows.Count > 0) {
                return DBCommonHelper.FillModel<ModelProductComment>(ds);
            }
            return null;
        }

        public List<ModelProductComment> GetModelList(int PageSize, int PageIndex, string strWhere, string strOrder, ref int RecordCount) {
            DataSet ds = this.GetList(PageSize, PageIndex, strWhere, strOrder, ref RecordCount);
            if (ds != null && ds.Tables[0].Rows.Count > 0) {
                return DBCommonHelper.FillModel<ModelProductComment>(ds);
            }
            return null;
        }

        public DataSet GetList(string strFiled, string strWhere) {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select " + strFiled + " ");
            strSql.Append(" FROM K_ProductComment ");
            if (strWhere.Trim() != "") {
                strSql.Append(" where 1=1 " + strWhere);
            }
            return DbHelperOleDb.Query(strSql.ToString());
        }

        public DataSet GetList(string strFiled, int Top, string strWhere, string filedOrder) {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0) {
                strSql.Append(" Top " + Top.ToString());
            }
            strSql.Append(" " + strFiled + " ");
            strSql.Append(" FROM K_ProductComment ");
            if (strWhere.Trim() != "") {
                strSql.Append(" where 1=1 " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperOleDb.Query(strSql.ToString());
        }

        public DataSet GetList(string strFiled, int PageSize, int PageIndex, string strWhere, string strOrder, ref int RecordCount) {
            StringBuilder strSqlCount = new StringBuilder();
            strSqlCount.Append("Select Count(*) as ID From K_ProductComment ");
            if (strWhere.Trim() != "") {
                strSqlCount.Append(" where 1=1 " + strWhere);
            }
            RecordCount = Convert.ToInt32("0" + DbHelperOleDb.GetSingle(strSqlCount.ToString()));
            if (PageIndex <= 1) {
                PageIndex = 1;
            }
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select Top " + PageSize + " " + strFiled + " from K_ProductComment where 1=1 ");
            if (strWhere.Trim() != "") {
                strSql.Append(" " + strWhere);
            }
            if (PageIndex > 1) {
                strSql.Append(" And ID Not In (Select Top " + ((PageIndex - 1) * PageSize) + " ID From K_ProductComment Where 1=1 ");
                if (strWhere.Trim() != "") {
                    strSql.Append(" " + strWhere);
                }
                if (!string.IsNullOrEmpty(strOrder)) {
                    strSql.Append(" Order by " + strOrder);
                }
                strSql.Append(")");
            }
            if (!string.IsNullOrEmpty(strOrder)) {
                strSql.Append(" Order by " + strOrder);
            }
            return DbHelperOleDb.Query(strSql.ToString());
        }
        #endregion K_ProductComment结束
    
    


    }
}
